// graceUI/components/graceReload.js
Component({
  properties: {
    reloadTxt: {
      type: Array,
      value: ['松开手指开始刷新', '数据刷新中', '数据已经刷新']
    },
    reloadBgColor: {
      type: Array,
      value : ['', '', '#63D2BC']
    },
    reloadColor: {
      type: Array,
      value :['#999999', '#63D2BC', '#FFFFFF']
    },
    width: {
      type: String,
      value : '750rpx'
    },
    marginLeft: {
      type: String,
      default: '0rpx'
    }
  },
  data: {
    reloadStatus: 5,
    height: 0,
    startY: 0,
    startTime: 0
  },
  methods: {
    /*
		刷新状态 : 
		5 无状态可以 进行下拉
		0 继续下拉刷新
		1 刷新中
		2 刷新结束
		3 不确认状态但下拉动作已经在执行
		*/
    touchstart: function (e) {
      // 如果滚动条位置不在最顶部禁止刷新
			if(e.scrollTop > 0){return ;}
			if(this.data.reloadStatus != 5){return ;}
      this.startY = e.moveY;
      this.startTime = new Date().getTime();
      this.setData({
        reloadStatus : 3,
        startY : e.moveY,
        startTime : new Date().getTime()
      });
    },
    touchmove: function (e) {
      if(this.data.reloadStatus == 3){
        // 检查时间
        var timer = new Date().getTime() - this.data.startTime;
        if (timer < 200) { return; }
        var moveY = e.moveY - this.data.startY;
        if (moveY > 100) { 
          this.setData({
            reloadStatus : 0,
            height : 100
          });
        }
      }
    },
    touchend: function (e) {
      switch(this.data.reloadStatus){
				case 0 :
          this.setData({reloadStatus: 1});
          this.triggerEvent('reload');
				break;
				case 1 :
				break;
				case 2 :
				break;
				case 3 :
          this.setData({ reloadStatus: 5});
				break;
			}
    },
    endReload: function () {
      this.setData({ reloadStatus: 2 });
      setTimeout(() => {
        this.setData({ reloadStatus: 5, height : 0 });
      }, 1000)
    },
    stoptouch : function(){},
    stopReload: function () {
      this.setData({ reloadStatus: 5, height: 0 });
    }
  }
})
